package com.hzh.page.controller.admin;


import com.hzh.page.common.enums.RightEnum;
import com.hzh.page.common.utils.R;
import com.hzh.page.pojo.PageCategory;
import com.hzh.page.service.AuthenticationMenuService;
import com.hzh.page.service.PageCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author HZH
 * @since 2022-11-11
 */
@Api(tags = "文章信息管理")
@RestController
@RequestMapping("/page/admin/pageCategory")
public class PageCategoryController {
    @Autowired
    private PageCategoryService pageCategoryService;
    @Autowired
    private AuthenticationMenuService menuService;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @ApiOperation("文章类型列表")
    @GetMapping("/pageCategoryList")
    public R pageCategoryList(HttpServletRequest request) {
        List<PageCategory> list = pageCategoryService.list();
        return R.ok().put("categoryList", list);
    }

    @ApiOperation("新增修改文章类型--传入id则为修改，否则新增")
    @PostMapping("/addOrUpdatePageCategory")
    public R addPageCategory(PageCategory pageCategory, HttpServletRequest request) {
        redisTemplate.delete("categoryList");
        if (pageCategory.getCategoryId() == null){
            menuService.hashAuthority(request, RightEnum.NEWS_MANAGE_ADD.getRoute_key());
            pageCategoryService.save(pageCategory);
            return R.ok("添加成功");
        }else {
            menuService.hashAuthority(request, RightEnum.NEWS_MANAGE_UPDATE.getRoute_key());
            pageCategoryService.updateById(pageCategory);
            return R.ok("修改成功");
        }
    }

    @ApiOperation("删除文章类型")
    @GetMapping("/deletePageCategory/{categoryId}")
    public R deletePageCategory(@PathVariable("categoryId") Long categoryId, HttpServletRequest request) {
        menuService.hashAuthority(request, RightEnum.NEWS_MANAGE.getRoute_key());
        pageCategoryService.removeById(categoryId);
        return R.ok("删除成功");
    }
}

